package com.jpro.module.system.controller.admin.membergroup;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.excel.core.util.ExcelUtils;
import com.jpro.module.system.controller.admin.membergroup.vo.*;
import com.jpro.module.system.convert.membergroup.MemberGroupConvert;
import com.jpro.module.system.dal.dataobject.membergroup.MemberGroupDO;
import com.jpro.module.system.service.membergroup.MemberGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 会员组")
@RestController
@RequestMapping("/system/membergroup")
@Validated
public class MemberGroupController {

    @Resource
    private MemberGroupService memberGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建会员组")
    @PreAuthorize("@ss.hasPermission('system:membergroup:create')")
    public CommonResult<Long> createMemberGroup(@Valid @RequestBody MemberGroupCreateReqVO createReqVO) {
        return success(memberGroupService.createMemberGroup(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新会员组")
    @PreAuthorize("@ss.hasPermission('system:membergroup:update')")
    public CommonResult<Boolean> updateMemberGroup(@Valid @RequestBody MemberGroupUpdateReqVO updateReqVO) {
        memberGroupService.updateMemberGroup(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除会员组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:membergroup:delete')")
    public CommonResult<Boolean> deleteMemberGroup(@RequestBody Long id) {
        memberGroupService.deleteMemberGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:membergroup:query')")
    public CommonResult<MemberGroupRespVO> getMemberGroup(@RequestParam("id") Long id) {
        MemberGroupDO memberGroup = memberGroupService.getMemberGroup(id);
        return success(MemberGroupConvert.INSTANCE.convert(memberGroup));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员组列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('system:membergroup:query')")
    public CommonResult<List<MemberGroupRespVO>> getMemberGroupList(@RequestParam("ids") Collection<Long> ids) {
        List<MemberGroupDO> list = memberGroupService.getMemberGroupList(ids);
        return success(MemberGroupConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员组分页")
    @PreAuthorize("@ss.hasPermission('system:membergroup:query')")
    public CommonResult<PageResult<MemberGroupDO>> getMemberGroupPage(@Valid MemberGroupPageReqVO pageVO) {
        PageResult<MemberGroupDO> pageResult = memberGroupService.getMemberGroupPage(pageVO);
        return success(pageResult);
    }


    @GetMapping("/list-all-simple")
    @Operation(summary = "获取会员组精简信息列表", description = "主要用于前端的下拉选项")
    public CommonResult<List<MemberGroupSimpleRespVO>> getSimpleRoleList() {
        // 获得列表
        MemberGroupExportReqVO exportReqVO = new MemberGroupExportReqVO();
        List<MemberGroupDO> list = memberGroupService.getMemberGroupList(exportReqVO);
        return success(MemberGroupConvert.INSTANCE.convertSimpleList(list));
    }

    @PostMapping("/assign-group-content-data-scope")
    @Operation(summary = "赋予会员组内容类权限")
    @PreAuthorize("@ss.hasPermission('system:membergroup:update')")
    public CommonResult<Boolean> assignRoleContentScope(@Valid @RequestBody PermissionAssignGroupContentDataScopeReqVO reqVO) {
        memberGroupService.assignGroupContentDataScope(reqVO.getRoleId(), reqVO.getDocDataScope(), reqVO.getSiteId(), reqVO.getDataScopeChannelIds());
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员组 Excel")
    @PreAuthorize("@ss.hasPermission('system:membergroup:export')")
    public void exportMemberGroupExcel(@Valid MemberGroupExportReqVO exportReqVO,
                                       HttpServletResponse response) throws IOException {
        List<MemberGroupDO> list = memberGroupService.getMemberGroupList(exportReqVO);
        // 导出 Excel
        List<MemberGroupExcelVO> datas = MemberGroupConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员组.xls", "数据", MemberGroupExcelVO.class, datas);
    }

}
